﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Foxit.PDF;
using Foxit.PDF.Imaging;
using Foxit.PDF.IO;
using Foxit.PDF.PageElements;
using Sustainalytics.Entities.RiskRatings;
using Sustainalytics.Reporting.Framework;

namespace Sustainalytics.RiskRatings.Reporting.Headers_and_Footers
{
    class MeiPageHeader : PageElement
    {
        public float Left { get; set; }
        public float Top { get; set; }
        public float Width { get; set; }
        public float Height { get; set; }
        public string CompanyName { get; set; }
        public string SubPeerGroup { get; set; }
        public string Country { get; set; }
        public string ExchangeTicker { get; set; }
        public string MeiName { get; set; }
        public string MeiDescription { get; set; }
        public string Description { get; set; }
        public string MeiIconName { get; set; }
        public float MeiIssueWeight { get; set; }
        public byte[] MeiImage { get; set; }
        public MaterialIssueType MeiType { get; set; }

        private Group Elements()
        {
            var elements = new Group();
            var middleSpace = (float)HorisontalSpacing.Large;
            var columnWidth = (Width - middleSpace) / 2;

            string reportTitle = "Sustainalytics ESG Risk Rating Report".ToUpper();
            float currentYOffset = Top;
            float startPos = 0;
            float descriptionHeight = 300f;

            elements.Add(new Label(reportTitle, Left, currentYOffset, columnWidth, (float)TextSizes.SubSectionTitle, Fonts.MontserratBold, (float)TextSizes.SubSectionTitle, TextAlign.Left, RgbColor.Black));

            var leftCompanyName = columnWidth + (float)HorisontalSpacing.Large;
            var companyNameSmallLenght = Fonts.RobotoMedium.GetTextWidth(CompanyName, 8);
            if (companyNameSmallLenght > columnWidth)
            {
                float companyNameFontSize = 7.5f;
                var textAreaObj = new TextArea(CompanyName, leftCompanyName, currentYOffset, columnWidth, companyNameFontSize * 2 + 10, Fonts.RobotoMedium, companyNameFontSize, TextAlign.Right, RgbColor.Black);
                while (textAreaObj.GetRequiredHeight() > 8)
                {
                    companyNameFontSize -= 0.5f;
                    textAreaObj = new TextArea(CompanyName, leftCompanyName, currentYOffset, columnWidth, companyNameFontSize * 2 + 10, Fonts.RobotoMedium, companyNameFontSize, TextAlign.Right, RgbColor.Black);
                }
                elements.Add(textAreaObj);
            }
            else
            {
                elements.Add(new Label(CompanyName, leftCompanyName, currentYOffset, columnWidth, 8f, Fonts.RobotoMedium, 8f, TextAlign.Right, RgbColor.Black));
            }

            /*
            var leftCompanyName = Width - 65f;
            if (CompanyName.Length >= 10 && CompanyName.Length < 15)
            {
                leftCompanyName = Width - 70f;
            }
            if (CompanyName.Length >= 15 && CompanyName.Length < 20)
            {
                leftCompanyName = Width - 120f;
            }
            if (CompanyName.Length > 20)
            {
                leftCompanyName = Width - 170f;
            }
            elements.Add(new Label(CompanyName, leftCompanyName, currentYOffset, columnWidth, (float)TextSizes.SubSectionTitle, Fonts.MontserratRegular, (float)TextSizes.SubSectionTitle, TextAlign.Left, RgbColor.Black));
            */

            currentYOffset = Top;
            if (MeiImage != null)
                elements.Add(new Image(ImageData.GetImage(MeiImage), Left, currentYOffset + 20, 0.5f));

            if (MeiName.Length > 20)
            {
                startPos = 15f;
                descriptionHeight += 50f;
            }
            var headerDescription = string.Empty;
            if (MeiType == MaterialIssueType.IdiosyncraticIssue)
            {
                headerDescription = "An idiosyncratic issue is an issue that was not initially deemed material at the subindustry level but becomes a material ESG issue for a company based on the occurrence of a Category 4 or 5 event. Idiosyncratic issues are represented only by the respective event indicator.";
            }
            else
            {
                headerDescription = MeiDescription ?? string.Empty;
            }
            currentYOffset += (float)TextSizes.RegularText + (float)VerticalSpacing.Small + 18;
            var headerText = new TextArea(headerDescription, Left + 250f + startPos, currentYOffset, columnWidth, descriptionHeight, Fonts.RobotoRegular, 8f, TextAlign.Left, RgbColor.Black);
            elements.Add(headerText);

            var meiNameLenght = Fonts.RobotoMedium.GetTextWidth(MeiName, 18);
 
            float meiNameFontSize = 13f;
            var textArea = new TextArea(MeiName, Left + 55, currentYOffset, columnWidth - 55, meiNameFontSize, Fonts.RobotoMedium, meiNameFontSize, TextAlign.Left, RgbColor.Black);
            while (textArea.GetRequiredHeight() > meiNameFontSize * 1.5f)
            {
                meiNameFontSize -= 0.5f;
                textArea = new TextArea(MeiName, Left + 55, currentYOffset, columnWidth - 55, meiNameFontSize, Fonts.RobotoMedium, meiNameFontSize, TextAlign.Left, RgbColor.Black);
            }
            elements.Add(textArea);

            currentYOffset += 25;
            elements.Add(new Label(string.Format("Contribution {0:0.0} %", MeiIssueWeight*100), Left + 57, currentYOffset, columnWidth, 8, Fonts.RobotoRegular, 8, TextAlign.Left, RgbColor.Black));
            return elements;
        }

        public override void Draw(PageWriter writer)
        {
            var elements = Elements();
            writer.Write_q_(true);
            elements.Draw(writer);
            writer.Write_Q(true);
        }
    }
}
